#!/usr/bin/perl -w
use strict;
use Bio::TreeIO;

my %expected_species = ( 'B3501' => 1,
                         'CNAG'  => 1,
                         'R265'   => 1,
                         'tmes'   => 0);

my $dir = shift @ARGV;
opendir(DIR, $dir) || die "$dir: $!";
for my $filename ( readdir(DIR) ) {
 # only process the files that end in '.0'
    next unless $filename =~ /\.0$/;
 my $input = new Bio::TreeIO(-file   => "$dir/$filename",
			     -format => "nhx");
    if( my $tree = $input->next_tree ) {
	for my $node ( $tree->get_nodes ) {
	    next if $node->is_Leaf;
	    my ($duplication) = $node->get_tag_values('D');
	    my ($species)     = $node->get_tag_values('S');
	    next unless defined $node->id && defined $species && defined $duplication;
	    if( $duplication eq 'Y' && $expected_species{ $species } ) {
		print $filename, "\t", $species,"\n";
	    }
	}
    }
}
